package com.heima.admin.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.heima.admin.service.AdNewsService;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.admin.dtos.NewsAuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yht
 * @date 2022/1/13 14:50
 */
@Service
@Slf4j
@Transactional
public class AdNewsServiceImpl implements AdNewsService {

    @Autowired
    private IWemediaClient iWemediaClient;
    /**
     * 查询自动审核失败的文章
     * @return
     */
    @Override
    public PageResponseResult findAutoAuditLose(NewsAuthDto newsAuthDto) {

        PageResponseResult autoAuditLose = iWemediaClient.findAutoAuditLose(newsAuthDto);
        if(autoAuditLose.getCode()!=200){
            throw new RuntimeException("findAutoAuditLose-远程查询失败");
        }
        return autoAuditLose;
    }

    /**
     * 根据id查询文章信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult findNewById(Integer id) {
        ResponseResult result = iWemediaClient.findNewById(id);
        if(result.getCode()!=200){
            throw new RuntimeException("findAutoAuditLose-远程查询失败 ");
        }
        return ResponseResult.okResult(result.getData());
    }

    /**
     * 文章新闻驳回
     * @param newsAuthDto
     * @return
     */
    @Override
    public ResponseResult authFail(NewsAuthDto newsAuthDto) {
        if(ObjectUtil.isEmpty(newsAuthDto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //远程调用查询
        ResponseResult responseResult = iWemediaClient.authFail(newsAuthDto);
        if(responseResult.getCode()!=200){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核成功
     * @param newsAuthDto
     * @return
     */
    @Override
    public ResponseResult authPass(NewsAuthDto newsAuthDto) {
        if(newsAuthDto==null){

           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //远程调用查询
        ResponseResult responseResult = iWemediaClient.authPass(newsAuthDto);
        return responseResult;
    }
}
